<?php
if ($msg != "default") echo $msg_output; 
if ($action == "view") {
$query_courses = "SELECT * FROM courses WHERE archive='N' ORDER BY jiu_course_id ASC";
$courses = mysql_query($query_courses, $connection) or die(mysql_error());
$row_courses = mysql_fetch_assoc($courses);
$totalRows_courses = mysql_num_rows($courses);

$query_archive = "SELECT * FROM courses WHERE archive='Y' ORDER BY jiu_course_id ASC";
$archive = mysql_query($query_archive, $connection) or die(mysql_error());
$row_archive = mysql_fetch_assoc($archive);
$totalRows_archive = mysql_num_rows($archive);
}

if ($action == "edit") {
$query_edit = "SELECT * FROM courses WHERE id='$id'";
$edit = mysql_query($query_edit, $connection) or die(mysql_error());
$row_edit = mysql_fetch_assoc($edit);
}

?>
<?php if ($action == "view") { 
if ($totalRows_courses > 0) { 
?>
<p>
<span class="icon"><img src="images/arrow_left.png" border="0" align="absmiddle" /></span><a href="<?php echo $_SERVER['HTTP_REFERER']; ?>">Back to Previous</a>
</p>
<p>
<span class="icon"><img src="images/page_add.png" border="0" align="absmiddle" /></span><a href="index.php?section=courses&action=add">Add a Course?</a></p>
<h2>Courses In Process</h2>
<table class="dataTable">
<tr>
    <th width="30%">Name</th>
    <th width="13%">CE</th>
    <th width="13%">ID</th>
    <th width="20%">Updated</th>
    <th width="13%">Approved</th>
    <!-- Uncomment for phase 2 
    <th>Completed</th>
    -->
    <th><?php if ($row_user['userLevel'] == "1") echo "Actions"; else echo "&nbsp;"; ?></th>
</tr>
<?php 
do { 
$query_id = sprintf("SELECT * FROM users WHERE id='%s'", $row_courses['did']); 
$id = mysql_query($query_id, $connection) or die(mysql_error());
$row_id = mysql_fetch_assoc($id);
$totalRows_id = mysql_num_rows($id);

$query_author = sprintf("SELECT * FROM users WHERE id='%s'", $row_courses['aid']); 
$author = mysql_query($query_author, $connection) or die(mysql_error());
$row_author = mysql_fetch_assoc($author);
$totalRows_author = mysql_num_rows($author);
?>
<tr style="background-color: <?php echo $color; ?>">
	<td nowrap="nowrap"><?php if (($row_courses['did'] == $row_user['id']) || ($row_user['userLevel'] == "1")) echo "<a href ='index.php?section=view_proposal&cid=".$row_courses['id']."'>"; echo $row_courses['jiu_course_id'].": ".$row_courses['name']; if (($row_courses['did'] == $row_user['id']) || ($row_user['userLevel'] <= "2"))  echo "</a>"; ?></td>
    <td class="data"><?php echo $row_author['firstName']." ".$row_author['lastName']; ?></td>
    <td class="data"><?php echo $row_id['firstName']." ".$row_id['lastName']; ?></td>
    <td class="data" nowrap="nowrap"><?php echo $row_courses['updated']; ?></td>
    <td class="data"><?php echo $row_courses['proposal_complete']; ?></td>
    <!-- Uncomment for phase 2 
    <td class="data"><?php //echo $row_courses['course_complete']; ?></td>
    -->
    <td class="data" nowrap="nowrap">
    <?php if (($row_courses['did'] == $row_user['id']) || ($row_user['userLevel'] == "1")) { ?>
    <table class="dataTableCompact">
    <tr>
    	<td class="icon"><img src="images/pencil.png" border="0" align="absmiddle" /></td>
        <td class="data"><a href="index.php?section=courses&action=edit&id=<?php echo $row_courses['id']; ?>">Edit Info</a></td>
    </tr>
    <tr>
    	<td class="icon"><img src="images/page_white_word.png" border="0" align="absmiddle" /></td>
        <td class="data"><a href="index.php?section=courses&action=export&cid=<?php echo $row_courses['id']; ?>">Export</a></td>
    </tr>
    <tr>
    	<td class="icon"><img src="images/printer.png" border="0" align="absmiddle" /></td>
        <td class="data"><a href="index.php?section=courses&action=print&cid=<?php echo $row_courses['id']; ?>">Print</a></td>
    </tr>    
	<?php } if ($row_user['userLevel'] == "1") { 
	if ($row_courses['archive'] == "N") {
	?>
    <tr>
    	<td class="icon"><img src="images/cog.png" border="0" align="absmiddle" /></td>
        <td class="data"><a href="javascript:confirmation('includes/process.inc.php?dbTable=courses&action=archive','id','<?php echo $row_courses['id']; ?>','Are you sure you want to archive <?php echo $row_courses['jiu_course_id']; ?>?');">Archive</a></td>
    </tr>
	<?php } ?>
    <tr>
	<td class="icon"><img src="images/bin_closed.png" border="0" align="absmiddle" /></td>
    <td class="data"><a href="javascript:confirmation('includes/process.inc.php?dbTable=courses&action=delete','id','<?php echo $row_courses['id']; ?>','Are you SURE you want to delete <?php echo $row_courses['jiu_course_id']; ?>?\n\nALL data relating to the course will be deleted including the project info, modules, assignments, themes!');">Delete</a></td>
	</tr>
    </table>
	<?php } ?>
    </td>
</tr>
<?php if ($color == $color1) { $color = $color2; } else { $color = $color1; } ?>
<?php } while ($row_courses = mysql_fetch_assoc($courses)); ?>
</table>
<?php } 
else echo "<div class='error_msg'>There are no courses in process in the Database. <a href='index.php?section=courses&amp;action=add'>Add one?</a></div>";
if ($totalRows_archive > 0) {
?>
<h2>Archived Courses</h2>
<table class="dataTable">
<tr>
    <th width="30%">Name</th>
    <th width="13%">CE</th>
    <th width="13%">ID</th>
    <th width="20%">Updated</th>
    <th width="13%">Approved</th>
    <!-- Uncomment for phase 2  
    <th>Completed</th>
    -->
    <th><?php if ($row_user['userLevel'] == "1") echo "Actions"; else echo "&nbsp;"; ?></th>
</tr>
<?php 
do { 
$query_id = sprintf("SELECT * FROM users WHERE id='%s'", $row_archive['did']); 
$id = mysql_query($query_id, $connection) or die(mysql_error());
$row_id = mysql_fetch_assoc($id);
$totalRows_id = mysql_num_rows($id);

$query_author = sprintf("SELECT * FROM users WHERE id='%s'", $row_archive['aid']); 
$author = mysql_query($query_author, $connection) or die(mysql_error());
$row_author = mysql_fetch_assoc($author);
$totalRows_author = mysql_num_rows($author);
?>
<tr style="background-color: <?php echo $color; ?>">
	<td nowrap="nowrap"><?php if (($row_archive['did'] == $row_user['id']) || ($row_user['userLevel'] == "1")) echo "<a href ='index.php?section=view_proposal&cid=".$row_archive['id']."'>"; echo $row_archive['jiu_course_id'].": ".$row_archive['name']; if (($row_archive['did'] == $row_user['id']) || ($row_user['userLevel'] <= "2"))  echo "</a>"; ?></td>
    <td class="data"><?php echo $row_author['firstName']." ".$row_author['lastName']; ?></td>
    <td class="data"><?php echo $row_id['firstName']." ".$row_id['lastName']; ?></td>
    <td class="data" nowrap="nowrap"><?php echo $row_archive['updated']; ?></td>
    <td class="data"><?php echo $row_archive['proposal_complete']; ?></td>
    <!-- Uncomment for phase 2 
    <td class="data"><?php //echo $row_archive['course_complete']; ?></td>
    -->
    <td class="data" nowrap="nowrap">
    <?php if (($row_archive['did'] == $row_user['id']) || ($row_user['userLevel'] == "1")) { ?>
    <table class="dataTableCompact">
    <tr>
    	<td class="icon"><img src="images/bin_closed.png" border="0" align="absmiddle" /></td>
        <td class="data"><a href="javascript:confirmation('includes/process.inc.php?dbTable=courses&action=delete','id','<?php echo $row_archive['id']; ?>','Are you sure you want to delete <?php echo $row_archive['jiu_course_id']; ?>? ALL data relating to the course will be deleted including the project info, modules, assignments, themes!');">Delete</a></td>
	</tr>
    </table>
	<?php } ?>
    </td>
</tr>
<?php if ($color == $color1) { $color = $color2; } else { $color = $color1; } ?>
<?php } while ($row_archive = mysql_fetch_assoc($archive)); ?>
</table>
<?php } 
} // end if "view"
if (($action == "add") || ($action == "edit")) { 

if ($row_user['userLevel'] <= "2") {
$query_id = "SELECT * FROM users"; 
$query_id .=  " WHERE userLevel='1' OR userLevel='2' ORDER BY lastName ASC";
$id = mysql_query($query_id, $connection) or die(mysql_error());
$row_id = mysql_fetch_assoc($id);
$totalRows_id = mysql_num_rows($id);
}

$query_author = "SELECT * FROM users WHERE userLevel='3' ORDER BY lastName ASC"; 
$author = mysql_query($query_author, $connection) or die(mysql_error());
$row_author = mysql_fetch_assoc($author);
$totalRows_author = mysql_num_rows($author);

?>
<p><span class="icon"><img src="images/arrow_left.png" border="0" align="absmiddle" /></span><a href="<?php echo $_SERVER['HTTP_REFERER']; ?>">Back to Previous</a></p>
<p><span class="icon"><img src="images/arrow_left.png" border="0" align="absmiddle" /></span><a href="index.php?section=courses&action=view">Back to Courses List</a></p>
<table class="dataTable">
<tr style="background-color: <?php echo $color1; ?>">
	<td class="dataLabel">JIU Course Number:</td>
    <td class="data"><input name="jiu_course_id" type="text" size="15" value="<?php if ($action == "edit") echo $row_edit['jiu_course_id']; ?>" /></td>
</tr>
<tr style="background-color: <?php echo $color2; ?>">
	<td class="dataLabel">Name:</td>
    <td class="data"><input name="name" type="text" size="50" value="<?php if ($action == "edit") echo $row_edit['name']; ?>" /></td>
</tr>
<tr style="background-color: <?php echo $color1; ?>">
	<td class="dataLabel">Content Expert:</td>
    <td class="data">
    <select name="aid" id="aid">
    	<?php do { ?>
    	<option value="<?php echo $row_author['id']; ?>" <?php if (($action == "edit") && ($row_edit['aid'] == $row_author['id'])) echo "SELECTED"; ?>><?php echo $row_author['firstName']." ".$row_author['lastName']; ?></option>
    	<?php } while ($row_author = mysql_fetch_assoc($author)); ?>
    </select>
    </td>
</tr>
<tr style="background-color: <?php echo $color2; ?>">
	<td class="dataLabel">Instructional Designer:</td>
    <?php if ($row_user['userLevel'] == "2") { ?>
    <td class="data"><?php echo $row_user['firstName']." ".$row_user['lastName']; ?><input name="did" type="hidden" value="<?php echo $row_user['id']; ?>" /></td>
    <?php } if ($row_user['userLevel'] == "1") { ?>
    <td class="data">
    <select name="did" id="did">
    	<?php do { ?>
    	<option value="<?php echo $row_id['id']; ?>" <?php if (($action == "edit") && ($row_edit['did'] == $row_id['id'])) echo "SELECTED"; ?>><?php echo $row_id['firstName']." ".$row_id['lastName']; ?></option>
    	<?php } while ($row_id = mysql_fetch_assoc($id)); ?>
    </select>
    </td>
    <?php } ?>
</tr>
<?php if ($action == "add") { ?>
<tr style="background-color: <?php echo $color1; ?>">
	<td class="dataLabel">Number of Modules:</td>
    <td class="data">
    <select name="modules" id="modules">
    	<option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
        <option value="4">4</option>
        <option value="5">5</option>
        <option value="6">6</option>
        <option value="7">7</option>
        <option value="8" selected="selected">8</option>
    </select>
    </td>
</tr>
<?php } ?>
</table>
<p style="margin-top: 1.25em;"><input id="Submit" name="Submit" type="submit" class="button" value="<?php if ($action == "edit") echo "Update"; else echo "Submit"; ?>"></p>
<?php }
if (($action == "export") || ($action == "print")) { ?>
<p>
<span class="icon"><img src="images/arrow_left.png" border="0" align="absmiddle" /></span><a href="<?php echo $_SERVER['HTTP_REFERER']; ?>">Back to Previous</a>
</p>
<p>
<span class="icon"><img src="images/arrow_left.png" border="0" align="absmiddle" /></span><a href="index.php?section=courses&action=view">Back to Courses</a></p>
<h2><?php if ($action == "print") echo "Print "; else echo "Export "; echo $row_courses['jiu_course_id'].": ".$row_courses['name']; ?></h2>
<table class="dataTable">
<tr>
	<th colspan="2">What do you wish to <?php echo $action; ?>?</th>
</tr>
<tr style="background-color: <?php echo $color; ?>">
	<td class="data" width="1%"><img src="images/<?php if ($action == "print") echo "printer.png"; else echo "page_white_word.png"; ?>" border="0" align="absmiddle" /></td>
    <td class="data"><a href="#" onClick="window.open('export.php?action=<?php echo $action; ?>&amp;cid=<?php echo $cid; ?>&amp;go=proposal','','height=1,width=1,toolbar=no,titlebar=no,status=no,menubar=yes,resizable=yes,scrollbars=yes,screenX=1,screenY=1'); return false;" title="Print the Proposal">The Proposal</a></td>
</tr>
<?php if ($totalRows_modules > 0) { ?>
<tr style="background-color: <?php echo $color1; ?>">
	<td class="data" width="1%"><img src="images/<?php if ($action == "print") echo "printer.png"; else echo "page_white_word.png"; ?>" border="0" align="absmiddle" /></td>
    <td class="data"><a href="#" onClick="window.open('export.php?action=<?php echo $action; ?>&amp;cid=<?php echo $cid; ?>&amp;go=project','','height=1,width=1,toolbar=no,titlebar=no,status=no,menubar=yes,resizable=yes,scrollbars=yes,screenX=1,screenY=1'); return false;" title="Print the Course Project Template">The Course Project Template</a></td>
</tr>
<tr style="background-color: <?php echo $color; ?>">
	<td class="data" width="1%"><img src="images/<?php if ($action == "print") echo "printer.png"; else echo "page_white_word.png"; ?>" border="0" align="absmiddle" /></td>
    <td class="data"><a href="#" onClick="window.open('export.php?action=<?php echo $action; ?>&amp;cid=<?php echo $cid; ?>&amp;go=syllabus','','height=1,width=1,toolbar=no,titlebar=no,status=no,menubar=yes,resizable=yes,scrollbars=yes,screenX=1,screenY=1'); return false;" title="Print the Syllabus Template">The Syllabus Template</a></td>
</tr>
<tr style="background-color: <?php echo $color1; ?>">
	<td class="data" width="1%"><img src="images/<?php if ($action == "print") echo "printer.png"; else echo "page_white_word.png"; ?>" border="0" align="absmiddle" /></td>
    <td class="data"><a href="#" onClick="window.open('export.php?action=<?php echo $action; ?>&amp;cid=<?php echo $cid; ?>&amp;go=home','','height=1,width=1,toolbar=no,titlebar=no,status=no,menubar=yes,resizable=yes,scrollbars=yes,screenX=1,screenY=1'); return false;" title="Print the Home Page Template">The Home Page Template</a></td>
</tr>
<tr style="background-color: <?php echo $color; ?>">
	<td class="data" width="1%"><img src="images/<?php if ($action == "print") echo "printer.png"; else echo "page_white_word.png"; ?>" border="0" align="absmiddle" /></td>
    <td class="data"><p>Module Templates:</p>
    <ul>
    <?php do { ?>
	<li><a href="#" onClick="window.open('export.php?action=<?php echo $action; ?>&amp;cid=<?php echo $cid; ?>&mid=<?php echo $row_modules['mid']; ?>&amp;go=module','','height=1,width=1,toolbar=no,titlebar=no,status=no,menubar=yes,resizable=yes,scrollbars=yes,screenX=1,screenY=1'); return false;" title="Print <?php echo "Module ".$row_modules['mid'].": "; if ($row_modules['module_title'] != "") echo $row_modules['module_title']; else echo "No Title Provided"; ?>"><?php echo "Module ".$row_modules['mid'].": "; if ($row_modules['module_title'] != "") echo $row_modules['module_title']; else echo "No Title Provided"; ?></a></li> 
	<?php } while ($row_modules = mysql_fetch_assoc($modules)); ?>
    </ul>
    </td>
</tr>
<?php } ?>
</table>
<?php } // end ($action == "export") ?>